<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\AdminRoleModel;
use App\Model\AdminUserModel;
use App\Http\Requests\AdminUserRequest;
use App\Http\Requests\AdminUserUpdate;
use DB;

/**
 * 后台用户控制器
 */
class AdminUser extends Controller
{
    /**
     * Display a listing of the resource.
     * 显示后台用户列表
     */
    public function index(Request $request)
    {
        //接收搜索条件
        $search = $request->input('search');

        if (empty($search)) {

            $where = [];

        } else {

            $where = [['name', 'like', "%$search%"]];
        }

        //
        $users = AdminUserModel::select('id', 'name', 'phone', 'email', 'status', 'created_at')->where($where)->paginate(5);

        return view('Admin.adminUser.admin-list', ['users' => $users, 'kw' => $search]);
    }

    /**
     * Show the form for creating a new resource.
     * 显示添加用户页面
     */
    public function create()
    {
        //
        $roles = AdminRoleModel::select('id', 'name', 'desc')->get();

        return view('Admin.adminUser.admin-add', ['roles' => $roles]);
    }

    /**
     * Store a newly created resource in storage.
     * 处理添加用户
     */
    public function store(AdminUserRequest $request)
    {
        //
        $user = new AdminUserModel();
        $user->name = $request->input('name');
        $user->password = password_hash($request->input('password'), PASSWORD_DEFAULT);
        $user->phone = $request->input('phone');
        $user->email = $request->input('email');

        $user->save();

        $user_role = [];
        foreach($request->input('role') as $value) {
          $user_role[] = [
              'role_id' => $value,
              'user_id' => $user->id

          ];

        }

        DB::table('shop_admin_user_roles')->insert($user_role);

        return back()->with('message', '添加用户成功！');
    }

    /**
     * Display the specified resource.
     * 显示用户详情
     */
    public function show($id)
    {
        //
        $user = AdminUserModel::find($id);

        return view('Admin.adminUser.admin-user-show', ['user' => $user]);
    }

    /**
     * Show the form for editing the specified resource.
     * 显示编辑页面
     */
    public function edit($id)
    {
        //
        $user = AdminUserModel::find($id);

        $roles = AdminRoleModel::all()->toArray();

        $userRole = $user->roles->toArray();

        //求出所有角色与该用户所拥有的角色的交集
        foreach($roles as $role) {

          $q[] = $role['id'];

        }

        foreach($userRole as $v) {

          $w[] = $v['id'];
        }

        $inter = array_intersect($q, $w);

        return view('Admin.adminUser.admin-edit', ['user' => $user, 'roles' => $roles, 'inter' => $inter]);

    }

    /**
     * Update the specified resource in storage.
     * 处理编辑用户
     */
    public function update(AdminUserUpdate $request, $id)
    {
        //
        $user = AdminUserModel::find($id);

        $user->name = $request->input('name');
        $user->phone = $request->input('phone');
        $user->email = $request->input('email');

        $user->save();

        DB::table('shop_admin_user_roles')->where('user_id', $id)->delete();

        $role_user = [];

        foreach ($request->input('role') as $value) {
            $role_user[] = [
                'role_id' => $value,
                'user_id' => $user->id
            ];
        }

        DB::table('shop_admin_user_roles')->insert($role_user);

        return back()->with('message', '编辑用户成功！');


    }

    /**
     * 处理禁用用户
     */
    public function stop(Request $request)
    {

        $res = AdminUserModel::where('id', $request->id)->update(['status' => 1]);

        return response()->json($res);
    }

    /**
     * 处理启用用户
     */
    public function start(Request $request)
    {

        $res = AdminUserModel::where('id', $request->id)->update(['status' => 0]);

        return response()->json($res);
    }

    /**
     * 显示后台更改密码
     */
    public function change($id)
    {
        $user = AdminUserModel::select('name')->where('id', $id)->first();

        return view('Admin.adminUser.change-password', ['user' => $user, 'id' => $id]);
    }

    /**
     * 处理更改密码
     */
    public function doChange(Request $request, $id)
    {
        $this->validate($request, [
            'password' => 'required|confirmed|min:6|max:12',
            'password_confirmation' => 'required',
        ],[
            'password.required' => '密码不能为空',
            'password.confirmed' => '两次密码输入不一致',
            'password.min' => '密码最小为6位，最大为12位',
            'password.max' => '密码最小为6位，最大为12位',
            'password_confirmation.required' => '确认密码不能为空',
        ]);

        $user = new AdminUserModel();

        $user->password = password_hash($request->input('password'), PASSWORD_DEFAULT);

        $user->update();

        return back()->with('message', '修改成功');
    }

    /**
     * Remove the specified resource from storage.
     * 处理删除管理员
     */
    public function destroy(Request $request)
    {

        $res = AdminUserModel::where('id', $request->id)->delete();

        return response()->json($res);

    }
}
